"""
FileName：excel_utils.py
Description：
Author：gaojiahui
Time：2025/8/15 8:53
Website：https://gitee.com/gao-jiahui
"""
import pandas as pd
from pathlib import Path
"""Excel数据读取工具类"""

class ExcelDataLoader:
    def __init__(self, file_path):
        self.file_path = Path(file_path)
        self.data = None

    def load_data(self,sheet_name="Sheet1"):
        """加载Excel数据"""
        if not self.file_path.exists():
            raise FileNotFoundError(f"Excel文件不存在:{self.file_path}")

        self.data = pd.read_excel(self.file_path,sheet_name=sheet_name)
        # 处理空值
        self.data.fillna('',inplace=True)
        return self.data

    def get_test_cases(self):
        """转换为测试用例列表"""
        if self.data is None:
            self.load_data()

        return self.data.to_dict('records')

    def get_data_by_column(self,column_name):
        """获取特定列数据"""
        if self.data is None:
            self.load_data()

        return self.data[column_name].tolist()

# 使用示例
login_loader = ExcelDataLoader("test_data/login_data.xlsx")
login_cases = login_loader.get_test_cases()